//
//  ForgetPasswdViewController.m
//  shuodi
//
//  Created by 孔晓寅 on 15/12/8.
//  Copyright © 2015年 Suzhou fish Information Technology Co., Ltd. All rights reserved.
//

#import "ForgetPasswdViewController.h"

@interface ForgetPasswdViewController (){
    NSTimer *showTimer;
    int count ;
}

@end

@implementation ForgetPasswdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"找回密码";
    
    [self initControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

-(void)loginclick:(id)sender{
    [self.view endEditing:YES];
    [self loginRequest];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void) initControl {
    [self.view setBackgroundColor:[utils getBackGroupColorGray]];
    
    CGFloat bottom = [utils getViewBottom:self.navigationController.navigationBar];
    
    _usernameEdit = [[UITextField alloc] init];
    UIView *userName = [self setEdit:@"icon_shouji_2x" withview:_usernameEdit withtext:@"" withplaceholder:@"请输入手机号" withY:bottom];
    _usernameEdit.text = @"";
    _usernameEdit.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:userName];
    
    _clear_button = [utils getButton:CGRectMake(userName.frame.size.width - _usernameEdit.frame.size.height/2 - 10 , 0, _usernameEdit.frame.size.height/2, _usernameEdit.frame.size.height) withName:@""];
    _clear_button.imageView.contentMode = UIViewContentModeScaleAspectFit;
    _clear_button.backgroundColor = [UIColor clearColor];
    [_clear_button setBackgroundImage:nil forState:UIControlStateHighlighted];
    [_clear_button setBackgroundImage:nil forState:UIControlStateSelected];
    [_clear_button setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateNormal];
    
    [userName addSubview:_clear_button];
    [_clear_button addTarget:self action:@selector(clearclick:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:userName] ;
    _passwdEdit = [[UITextField alloc] init];
    UIView *passWord = [self setEdit:@"icon_yanzhengma_2x" withview:_passwdEdit withtext:@"" withplaceholder:@"请输入验证码" withY:bottom];
    _passwdEdit.text = @"";
    _passwdEdit.keyboardType = UIKeyboardTypeNumberPad;
    [self.view addSubview:passWord];
    
    _getcode_button = [utils getButton:CGRectMake(self.view.frame.size.width - _passwdEdit.frame.size.height*2 , bottom+1, _passwdEdit.frame.size.height*2, _passwdEdit.frame.size.height-2) withName:@"获取验证码"];
    _getcode_button.titleLabel.font = [UIFont systemFontOfSize:13];
    _getcode_button.backgroundColor = [UIColor clearColor];
    [_getcode_button setTitleColor:[utils getButtonColor] forState:UIControlStateNormal];
    [_getcode_button setTitleColor:[utils getTextColorGray] forState:UIControlStateDisabled];
    [self.view addSubview:_getcode_button];
    [_getcode_button addTarget:self action:@selector(getverification:) forControlEvents: UIControlEventTouchUpInside];
    
    bottom = [utils getViewBottom:passWord] ;
    
    _newpasswdEdit = [[UITextField alloc] init];
    UIView *newpassWord = [self setEdit:@"icon_mima_2x" withview:_newpasswdEdit withtext:@"" withplaceholder:@"请输入密码" withY:bottom];
    _newpasswdEdit.text = @"";
    _newpasswdEdit.secureTextEntry = YES;
    [self.view addSubview:newpassWord];
    
    bottom = [utils getViewBottom:newpassWord] ;
    _newcomfirmpasswdEdit = [[UITextField alloc] init];
    UIView *newcomfirmpasswd = [self setEdit:@"icon_mima_2x" withview:_newcomfirmpasswdEdit withtext:@"" withplaceholder:@"请再次确认密码" withY:bottom];
    _newcomfirmpasswdEdit.text = @"";
    _newcomfirmpasswdEdit.secureTextEntry = YES;
    [self.view addSubview:newcomfirmpasswd];
    
    bottom = [utils getViewBottom:newcomfirmpasswd] + 20;
    
    _register_button = [utils getButton:CGRectMake(OFFSET_TWN, bottom, self.view.frame.size.width-OFFSET_TWN*2, _passwdEdit.frame.size.height) withName:@"确认"];
    [self.view addSubview:_register_button];
    [_register_button addTarget:self action:@selector(loginclick:) forControlEvents: UIControlEventTouchUpInside];
    
}

-(IBAction)clearclick:(id)sender{
    
    _usernameEdit.text = @"";
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y with_w:(CGFloat)width{
    
    CGFloat left_x = 0;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(left_x, point_y, width-left_x*2, 44)];
    [view setBackgroundColor:[UIColor whiteColor]];
    
    UIImage *img = [UIImage imageNamed:img_url];
    CGFloat icon_img_h = view.frame.size.height - 24;
    CGFloat icon_img_w = img.size.width/img.size.height*icon_img_h;
    UIImageView *icon_img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 12, icon_img_w, icon_img_h)];
    [icon_img setImage:img];
    
    [subview setFrame:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)];
    subview.text = text;
    subview.textColor = [UIColor blackColor];
    subview.font = [UIFont systemFontOfSize:15];
    subview.placeholder = placeholder;
    [subview.layer setCornerRadius:5];
    [subview.layer setBorderWidth:0];
    [subview.layer setBorderColor:[[utils getBorderColorGray] CGColor]];
    [subview setBackgroundColor:[UIColor whiteColor]];
    subview.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 34, 0)];
    subview.leftViewMode = UITextFieldViewModeAlways;
    subview.delegate = self;
    //    [subview addTarget:self action:@selector(editingChanged:) forControlEvents:UIControlEventEditingChanged];
    subview.returnKeyType = UIReturnKeyDone;
    
    [view addSubview:subview];
    [view addSubview:icon_img];
    
    UIView *lbl = [[UIView alloc] initWithFrame:CGRectMake(0, 43, view.frame.size.width, 0.5)];
    lbl.backgroundColor =  [utils getLineColorGray];
    [view addSubview:lbl];
    
    return view;
    
}

-(UIView *)setEdit:(NSString *)img_url withview:(UITextField*)subview withtext:(NSString*)text withplaceholder:(NSString*)placeholder withY:(CGFloat)point_y{
    
    return [self setEdit:img_url withview:subview withtext:text withplaceholder:placeholder withY:point_y with_w:self.view.frame.size.width];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    CGRect frame = [textField superview].frame;
    //    CGFloat offset_y = self.myScrollView.contentOffset.y;
    
    //在这里我多加了62，（加上了输入中文选择文字的view高度）这个依据自己需求而定
    int offset = (frame.origin.y+120)-(self.view.frame.size.height-216.0);//键盘高度216
    
    if (offset>0) {
        
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        
        [UIView setAnimationDuration:0.30f];//动画持续时间
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
        [UIView commitAnimations];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    //输入框编辑完成以后，当键盘即将消失时，将视图恢复到原始状态
    self.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    
}

-(void)loginRequest{
    
    [_usernameEdit resignFirstResponder];
    [_passwdEdit resignFirstResponder];
    [_newpasswdEdit resignFirstResponder];
    
    if ([_usernameEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if (![utils checkTelNumber:_usernameEdit.text]) {
        [OMGToast showWithText:@"请输入正确手机号" bottomOffset:50 duration:1];
        return;
    }
    
    if ([_newpasswdEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入密码" bottomOffset:50 duration:1];
        return;
    }
    
//    if ([_newcomfirmpasswdEdit.text isEqualToString:@""]) {
//        [OMGToast showWithText:@"确认密码不能为空" bottomOffset:50 duration:1];
//        return;
//    }
    
    if ([_newcomfirmpasswdEdit.text isEqualToString:_newpasswdEdit.text]) {
    }else{
        [OMGToast showWithText:@"请再次确认密码" bottomOffset:50 duration:1];
        return;
    }
    
    if ([_passwdEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入验证码" bottomOffset:50 duration:1];
        return;
    }
    
    
    [HttpNetWorkUtil requestgetPwd:@{@"phone":_usernameEdit.text,@"newPwd":_newcomfirmpasswdEdit.text ,@"code":_passwdEdit.text} success:^(id json) {
        [OMGToast showWithText:@"密码重置成功" bottomOffset:50 duration:2];
        [self.navigationController popViewControllerAnimated:YES];
    } fail:^{}];
}

-(void)getverification:(id)sender {
    
    [_usernameEdit resignFirstResponder];
    [_passwdEdit resignFirstResponder];
    [_newpasswdEdit resignFirstResponder];
    
    if ([_usernameEdit.text isEqualToString:@""]) {
        [OMGToast showWithText:@"请输入手机号码" bottomOffset:50 duration:1];
        return;
    }
    
    if (![utils checkTelNumber:_usernameEdit.text]) {
        [OMGToast showWithText:@"请输入正确的手机号码" bottomOffset:50 duration:1];
        return;
    }
    
    [HttpNetWorkUtil requestgetVerificationCode:@{@"phone":_usernameEdit.text,@"codeType":@"2"} success:^(id json) {
        [self initTimer];
    } fail:^{}];
    
}

-(void)initTimer
{
    count = 60;
    //时间间隔
    NSTimeInterval timeInterval =1.0 ;
    //定时器
    _getcode_button.enabled = NO;
    [_getcode_button setTitle:[NSString stringWithFormat:@"重新发送(%d)",count] forState:UIControlStateNormal];
    showTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval
                                                 target:self
                                               selector:@selector(handleTimer:)
                                               userInfo:nil
                                                repeats:YES];
}

-(void)handleTimer:(id)sender {
    count --;
    if (count <= 0 ) {
        [showTimer invalidate];
        _getcode_button.enabled = YES;
        [_getcode_button setTitle:@"重新获取" forState:UIControlStateNormal];
    } else {
        [_getcode_button setTitle:[NSString stringWithFormat:@"重新发送(%d)",count] forState:UIControlStateNormal];
    }
}

@end
